using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using OgilvyOne.Model;
using OgilvyOne.IDAL;

namespace OgilvyOne.SqlServerDAL
{
    public class SVWTracking : IVWTracking
    {
        // Methods
        public bool DeleteByIdentity(int id)
        {
            bool flag = false;
            int num = 0;
            string cmdText = "";
            cmdText = "DELETE FROM VWTracking WHERE id=@id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 10) };
            cmdParms[0].Value = id;
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        num = SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, cmdText, cmdParms);
                        transaction.Commit();
                        if (num > 0)
                        {
                            flag = true;
                        }
                    }
                    catch (SqlException exception)
                    {
                        transaction.Rollback();
                        Console.WriteLine(exception.Message);
                    }
                    return flag;
                }
            }
        }

        public MVWTracking GetRowByIdentity(int id)
        {
            MVWTracking tracking = null;
            string cmdText = "";
            cmdText = "SELECT id,TrackingCode,UserIP,isrdt,mark FROM VWTracking WHERE id=@id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 10) };
            cmdParms[0].Value = id;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, cmdParms))
            {
                while (reader.Read())
                {
                    try
                    {
                        tracking = new MVWTracking();
                        tracking.Id = Convert.ToInt32(reader[0]);
                        tracking.TrackingCode = Convert.ToString(reader[1]);
                        tracking.UserIP = Convert.ToString(reader[2]);
                        tracking.Isrdt = Convert.ToString(reader[3]);
                        tracking.Mark = Convert.ToString(reader[4]);
                    }
                    catch (SqlException exception)
                    {
                        Console.WriteLine(exception.Message);
                    }
                }
            }
            return tracking;
        }

        public DataSet GetRows(string condition)
        {
            DataSet dataSet = new DataSet();
            string str = "SELECT id,TrackingCode,UserIP,isrdt,mark FROM VWTracking " + condition;
            using (SqlDataAdapter adapter = new SqlDataAdapter())
            {
                try
                {
                    adapter.SelectCommand = new SqlCommand();
                    adapter.SelectCommand.CommandText = str;
                    adapter.SelectCommand.Connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC);
                    adapter.Fill(dataSet);
                }
                catch (SqlException exception)
                {
                    Console.WriteLine(exception.Message);
                }
            }
            return dataSet;
        }

        public int Insert(MVWTracking data)
        {
            int num = 0;
            string cmdText = "INSERT INTO VWTracking(TrackingCode,UserIP,mark) VALUES(@TrackingCode, @UserIP, @mark)";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TrackingCode", SqlDbType.NVarChar, 50), new SqlParameter("@UserIP", SqlDbType.NVarChar, 50), new SqlParameter("@mark", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = data.TrackingCode;
            cmdParms[1].Value = data.UserIP;
            cmdParms[2].Value = data.Mark;
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        num = SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, cmdText, cmdParms);
                        transaction.Commit();
                    }
                    catch (SqlException exception)
                    {
                        transaction.Rollback();
                        Console.WriteLine(exception.Message);
                    }
                    return num;
                }
            }
        }

        public bool ModifyByIdentity(MVWTracking data)
        {
            bool flag = false;
            int num = 0;
            string cmdText = "";
            cmdText = "UPDATE VWTracking SET TrackingCode=@TrackingCode,UserIP=@UserIP,mark=@mark WHERE id=@id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 10), new SqlParameter("@TrackingCode", SqlDbType.NVarChar, 50), new SqlParameter("@UserIP", SqlDbType.NVarChar, 50), new SqlParameter("@mark", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = data.Id;
            cmdParms[1].Value = data.TrackingCode;
            cmdParms[2].Value = data.UserIP;
            cmdParms[3].Value = data.Mark;
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        num = SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, cmdText, cmdParms);
                        transaction.Commit();
                        if (num > 0)
                        {
                            flag = true;
                        }
                    }
                    catch (SqlException exception)
                    {
                        transaction.Rollback();
                        Console.WriteLine(exception.Message);
                    }
                    return flag;
                }
            }
        }
    }
}